package com.junlong0716.enjoylearning.ui

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewPager
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.Toast
import com.blankj.utilcode.util.ToastUtils
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.adapter.StudyAdapter
import com.junlong0716.enjoylearning.entity.rxevent.ChangeTabEvent
import com.junlong0716.enjoylearning.entity.rxevent.MusicPlayComplete
import com.junlong0716.module.common.base.BaseFragment
import com.junlong0716.module.common.rxbus.RxBus
import com.junlong0716.module.common.rxbus.Subscribe
import com.junlong0716.module.common.rxbus.ThreadMode
import kotlinx.android.synthetic.main.activity_audio.*
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView


/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 下午8:05 2018/5/6
 *@modified by:
 */
class StudyFragment : BaseFragment<StudyPresenter>(), StudyContract.View {
    private lateinit var mTitleDataList: ArrayList<String>
    private lateinit var mFragments: ArrayList<Fragment>
    private lateinit var mLLRoot: LinearLayout
    private lateinit var mViewPager: ViewPager
    override fun getLayoutId(): Int = R.layout.fragment_study


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun eventBus(s: ChangeTabEvent) {
        if (s.code == 0xfafb) {
            changeTab()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //RxBus.getDefault().register(this)
    }
    override fun initViews(mRootView: View?) {
        mTitleDataList = ArrayList()
        mTitleDataList.add("学习记录")
        mTitleDataList.add("知识清单")
        val magicIndicator = mRootView!!.findViewById<MagicIndicator>(R.id.magic_indicator)
        mViewPager = mRootView.findViewById<ViewPager>(R.id.viewpager)
        mLLRoot = mRootView.findViewById<LinearLayout>(R.id.ll_root)
        mFragments = ArrayList()
        mFragments.add(StudyRecordFragment())
        mFragments.add(KnowledgeListFragment())

        mViewPager.adapter = StudyAdapter(activity!!.supportFragmentManager, mFragments)

        val commonNavigator = CommonNavigator(activity)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getTitleView(p0: Context?, p1: Int): IPagerTitleView {
                val colorTransitionPagerTitleView = ColorTransitionPagerTitleView(context)
                colorTransitionPagerTitleView.normalColor = Color.GRAY
                colorTransitionPagerTitleView.selectedColor = ContextCompat.getColor(activity!!, R.color.colorAccent)
                colorTransitionPagerTitleView.text = mTitleDataList[p1]
                colorTransitionPagerTitleView.setOnClickListener {
                    mViewPager.currentItem = p1
                }
                return colorTransitionPagerTitleView
            }

            override fun getCount(): Int = if (mTitleDataList == null) 0 else mTitleDataList.size

            override fun getIndicator(p0: Context?): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.lineHeight = 7f
                indicator.setColors(ContextCompat.getColor(activity!!, R.color.colorAccent))
                indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                return indicator
            }
        }
        magicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, mViewPager)

        mRootView.findViewById<FrameLayout>(R.id.fl_add_notes).visibility = View.GONE
        mRootView.findViewById<FrameLayout>(R.id.fl_add_notes).setOnClickListener {
            startActivity(Intent(activity, AddNotesActivity::class.java))
        }
    }

    override fun lazyFetchData() {
    }

    override fun attachPresenter() {
        mPresenter = StudyPresenter()
        mPresenter!!.attachView(this)
    }

    fun changeTab() {
        mViewPager.currentItem = 0
    }

    override fun onDestroy() {
        super.onDestroy()
        //RxBus
    }
}